package web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import web.pojo.ClassSchedule;
import web.pojo.User;
import web.service.ClassScheduleService;
import web.service.UserService;

@Controller
@RequestMapping("classSchedule")
public class ClassScheduleController {

    @Autowired
    ClassScheduleService service;
    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping("scheduleList")
    public List<ClassSchedule> fetchScheduleByWeek(Integer week, Long teacher_id, HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("UID");

        User user = userService.simpleUserInfo(id);

        switch (user.getType()) {
            // 管理员
            case 1:
                return new ArrayList<>();
            // 老师
            case 2:
                return service.fetchScheduleByWeekAndTeacher(week, id);
            // 学生
            case 3:
                return service.fetchScheduleByWeekAndStudent(week, teacher_id, id);
            default:
                return new ArrayList<>();
        }
    }

    @ResponseBody
    @RequestMapping("addSchedule")
    public boolean insertSchedule(Long courseId, Long classId,
            Long teacherId, Long classroomId,
            String[] week,
            Integer day, Integer section) {

        return service.insertSchedule(courseId, classId, teacherId, classroomId, week, day, section);
    }

    @ResponseBody
    @RequestMapping("schedule")
    public List<ClassSchedule> findSchedulesList(Long teacherId, Long courseId,
            Long classId, Long classroomId, Integer week, HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("UID");

        User user = userService.simpleUserInfo(id);
        if (user.getType() != 1) {
            return new ArrayList<>();
        }
        return service.fetchScheduleList(teacherId, courseId, classId, classroomId, week);
    }

    @ResponseBody
    @RequestMapping("deleteSchedule")
    public boolean deleteScheduleItemById(Long id, HttpServletRequest req) {
        Long uid = (Long) req.getSession().getAttribute("UID");

        User user = userService.simpleUserInfo(uid);
        if (user.getType() != 1) {
            return false;
        }

        if (id == null) {
            return false;
        }

        return service.deleteScheduleItemById(id);
    }

    @ResponseBody
    @RequestMapping("updateSchedule")
    public boolean updateSchedule(Long id, Long teacherId, Long classroomId, Integer week, Integer day,
            Integer section, HttpServletRequest req) {
        Long uid = (Long) req.getSession().getAttribute("UID");

        User user = userService.simpleUserInfo(uid);
        if (user.getType() != 1) {
            return false;
        }

        if (id == null) {
            return false;
        }
        return service.updateSchedule(id, teacherId, classroomId, week, day, section);
    }
}
